package com.hanoi.wiki.controller.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanoi.wiki.controller.BlocksController;
import com.hanoi.wiki.exception.FunctionalException;
import com.hanoi.wiki.manager.BlocksManager;
import com.hanoi.wiki.model.json.BaseJsonObject;
import com.hanoi.wiki.model.json.BlocksJson;
import com.hanoi.wiki.model.json.ResponseJsonObject;

@Controller
@RequestMapping(value = "/blk")
public class BlocksControllerImpl extends BaseControllerImpl implements BlocksController {
	
	@Autowired
	private BlocksManager blocksManager;

	public @ResponseBody ResponseJsonObject saveBlocks(@RequestBody BlocksJson blocks) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		int id = 0;
		try {
			id = blocksManager.save(blocks);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		blocks.setId(id);
		objects.add(blocks);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject removeBlocks(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		BlocksJson json = null;
		try {
			json = blocksManager.get(id);
			blocksManager.remove(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}		
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject updateBlocks(@RequestBody BlocksJson blocks) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			blocksManager.update(blocks);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(blocks);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getBlocks(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		BlocksJson json = null;
		try {
			json = blocksManager.get(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getBlocksList() {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			objects.addAll(blocksManager.getList());
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

}
